﻿Public Class frmSysManageAddUser

    Private m_broker As ClsBroker
    Private Sub frmSysManageAddUser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        m_broker = New ClsBroker()

    End Sub

    Private Sub btnSaveUser_Click(sender As Object, e As EventArgs) Handles btnSaveUser.Click
        Dim tmpUserName As String = Me.txtUserName.Text.Trim()
        Dim tmpUserPass As String = Me.txtUserPass.Text.Trim()
        Dim tmpIfAdmin As Boolean = Me.chkIfAdmin.Checked
        Dim tmpDb As ClsUserDbAccess = New ClsUserDbAccess(sysAppInfoConnectionString)

        '判断是否可以保存
        If String.IsNullOrEmpty(tmpUserName) Then
            MessageBox.Show("登录名称必须填写。且不大于20个字，不能包含空格。", "新增用户")
            Return
        End If

        '判断名字是否重复
        Dim tmpNameList As List(Of ClsUser) = tmpDb.GetUserByName(tmpUserName)
        If tmpNameList IsNot Nothing Then 'OrElse tmpNameList.Count > 0 Then
            '有同名者
            MessageBox.Show("登录名称已存在，登录名称不能重复。", "新增用户")
            Return
        End If

        If String.IsNullOrEmpty(tmpUserPass) Then
            MessageBox.Show("登录密码必须填写。不大于20个字符。", "新增用户")
            Return
        End If

        '准备保存
        Dim tmpUser As ClsUser = New ClsUser()
        tmpUser.UserId = 0
        tmpUser.UserName = tmpUserName
        tmpUser.UserPassword = Md5Encrypt(tmpUserPass)
        tmpUser.UserIfAdmin = tmpIfAdmin


        Dim tmpR As Int32 = tmpDb.InsertUser(tmpUser)
        If tmpR = 0 Then
            MessageBox.Show("保存用户时出错。"， "新增用户")
            Return
        End If
        tmpUser.UserId = tmpR


        '引发广播，
        Dim eArg As TransEventArg = New TransEventArg()
        eArg.Msg = "AddUser"
        eArg.Obj = tmpUser

        m_broker.BroadCastEvent(eArg)

        '关闭自身
        Me.Close()
    End Sub
End Class